/*
 * @(#)HomeView.java
 *
 * @author <a href="gubarevs@apollo.lv">Denis Gubarev</a>
 */

package com.gshappcore.presentation.view;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.gshappcore.presentation.navigation.Navigation;

public abstract class HomeView implements EntryPoint, HistoryListener {


    public abstract String defineHomeView();

    public abstract Navigation defineNavigation();

    public abstract String defineDivId();


    public void onModuleLoad() {
        String token = History.getToken();
        onHistoryChanged(token);
        History.addHistoryListener(this);
        History.fireCurrentHistoryState();
    }

    private void showView(View view) {
        view.destroy();
        RootPanel.get(defineDivId()).clear();
        view.init();
    }

    Navigation navigation = defineNavigation();

    public void onHistoryChanged(String token) {
        String viewName = getViewName(token);
        View homeView = navigation.getView(viewName);
        History.newItem(viewName);
        showView(homeView);
    }

    private String getViewName(String token) {
        return isStringEmpty(token) ? defineHomeView() : token;
    }

    private boolean isStringEmpty(String s) {
        return (s == null || s.equals(""));
    }
}
